home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cp / tree.c < prev    next >
C/C++ Source or Header  |  1994-07-13  |  48KB  |  1,764 lines

  1. /* Language-dependent node constructors for parse phase of GNU compiler.
  2.    Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include "obstack.h"
  24. #include "tree.h"
  25. #include "cp-tree.h"
  26. #include "flags.h"
  27.  
  28. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  29.  
  30. /* Return nonzero if REF is an lvalue valid for this language.
  31.    Lvalues can be assigned, unless they have TREE_READONLY.
  32.    Lvalues can have their address taken, unless they have DECL_REGISTER.  */
  33.  
  34. int
  35. lvalue_p (ref)
  36.      tree ref;
  37. {
  38.   register enum tree_code code = TREE_CODE (ref);
  39.  
  40.   if (language_lvalue_valid (ref))
  41.     {
  42.       if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
  43.     return 1;
  44.       
  45.       switch (code)
  46.     {
  47.       /* preincrements and predecrements are valid lvals, provided
  48.          what they refer to are valid lvals. */
  49.     case PREINCREMENT_EXPR:
  50.     case PREDECREMENT_EXPR:
  51.     case COMPONENT_REF:
  52.     case SAVE_EXPR:
  53.       return lvalue_p (TREE_OPERAND (ref, 0));
  54.  
  55.     case STRING_CST:
  56.       return 1;
  57.  
  58.     case VAR_DECL:
  59.       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
  60.           && DECL_LANG_SPECIFIC (ref)
  61.           && DECL_IN_AGGR_P (ref))
  62.         return 0;
  63.     case INDIRECT_REF:
  64.     case ARRAY_REF:
  65.     case PARM_DECL:
  66.     case RESULT_DECL:
  67.     case ERROR_MARK:
  68.       if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
  69.           && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
  70.         return 1;
  71.       break;
  72.  
  73.     case TARGET_EXPR:
  74.     case WITH_CLEANUP_EXPR:
  75.       return 1;
  76.  
  77.       /* A currently unresolved scope ref.  */
  78.     case SCOPE_REF:
  79.       my_friendly_abort (103);
  80.     case OFFSET_REF:
  81.       if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
  82.         return 1;
  83.       return lvalue_p (TREE_OPERAND (ref, 0))
  84.         && lvalue_p (TREE_OPERAND (ref, 1));
  85.       break;
  86.  
  87.     case COND_EXPR:
  88.       return (lvalue_p (TREE_OPERAND (ref, 1))
  89.           && lvalue_p (TREE_OPERAND (ref, 2)));
  90.  
  91.     case MODIFY_EXPR:
  92.       return 1;
  93.  
  94.     case COMPOUND_EXPR:
  95.       return lvalue_p (TREE_OPERAND (ref, 1));
  96.     }
  97.     }
  98.   return 0;
  99. }
  100.  
  101. /* Return nonzero if REF is an lvalue valid for this language;
  102.    otherwise, print an error message and return zero.  */
  103.  
  104. int
  105. lvalue_or_else (ref, string)
  106.      tree ref;
  107.      char *string;
  108. {
  109.   int win = lvalue_p (ref);
  110.   if (! win)
  111.     error ("non-lvalue in %s", string);
  112.   return win;
  113. }
  114.  
  115. /* INIT is a CALL_EXPR which needs info about its target.
  116.    TYPE is the type that this initialization should appear to have.
  117.  
  118.    Build an encapsulation of the initialization to perform
  119.    and return it so that it can be processed by language-independent
  120.    and language-specific expression expanders.
  121.  
  122.    If WITH_CLEANUP_P is nonzero, we build a cleanup for this expression.
  123.    Otherwise, cleanups are not built here.  For example, when building
  124.    an initialization for a stack slot, since the called function handles
  125.    the cleanup, we would not want to do it here.  */
  126. tree
  127. build_cplus_new (type, init, with_cleanup_p)
  128.      tree type;
  129.      tree init;
  130.      int with_cleanup_p;
  131. {
  132.   tree slot = build (VAR_DECL, type);
  133.   tree rval = build (NEW_EXPR, type,
  134.              TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
  135.   TREE_SIDE_EFFECTS (rval) = 1;
  136.   TREE_ADDRESSABLE (rval) = 1;
  137.   rval = build (TARGET_EXPR, type, slot, rval, 0);
  138.   TREE_SIDE_EFFECTS (rval) = 1;
  139.   TREE_ADDRESSABLE (rval) = 1;
  140.  
  141. #if 0
  142.   if (with_cleanup_p && TYPE_NEEDS_DESTRUCTOR (type))
  143.     {
  144.       TREE_OPERAND (rval, 2) = error_mark_node;
  145.       rval = build (WITH_CLEANUP_EXPR, type, rval, 0,
  146.             build_delete (TYPE_POINTER_TO (type),
  147.                   build_unary_op (ADDR_EXPR, slot, 0),
  148.                   integer_two_node,
  149.                   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0));
  150.       TREE_SIDE_EFFECTS (rval) = 1;
  151.       TREE_ADDRESSABLE (rval) = 1;
  152.     }
  153. #endif
  154.   return rval;
  155. }
  156.  
  157. /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
  158.    these CALL_EXPRs with tree nodes that will perform the cleanups.  */
  159.  
  160. tree
  161. break_out_cleanups (exp)
  162.      tree exp;
  163. {
  164.   tree tmp = exp;
  165.  
  166.   if (TREE_CODE (tmp) == CALL_EXPR
  167.       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
  168.     return build_cplus_new (TREE_TYPE (tmp), tmp, 1);
  169.  
  170.   while (TREE_CODE (tmp) == NOP_EXPR
  171.      || TREE_CODE (tmp) == CONVERT_EXPR
  172.      || TREE_CODE (tmp) == NON_LVALUE_EXPR)
  173.     {
  174.       if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
  175.       && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
  176.     {
  177.       TREE_OPERAND (tmp, 0)
  178.         = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
  179.                    TREE_OPERAND (tmp, 0), 1);
  180.       break;
  181.     }
  182.       else
  183.     tmp = TREE_OPERAND (tmp, 0);
  184.     }
  185.   return exp;
  186. }
  187.  
  188. /* Recursively perform a preorder search EXP for CALL_EXPRs, making
  189.    copies where they are found.  Returns a deep copy all nodes transitively
  190.    containing CALL_EXPRs.  */
  191.  
  192. tree
  193. break_out_calls (exp)
  194.      tree exp;
  195. {
  196.   register tree t1, t2;
  197.   register enum tree_code code;
  198.   register int changed = 0;
  199.   register int i;
  200.  
  201.   if (exp == NULL_TREE)
  202.     return exp;
  203.  
  204.   code = TREE_CODE (exp);
  205.  
  206.   if (code == CALL_EXPR)
  207.     return copy_node (exp);
  208.  
  209.   /* Don't try and defeat a save_expr, as it should only be done once. */
  210.     if (code == SAVE_EXPR)
  211.        return exp;
  212.  
  213.   switch (TREE_CODE_CLASS (code))
  214.     {
  215.     default:
  216.       abort ();
  217.  
  218.     case 'c':  /* a constant */
  219.     case 't':  /* a type node */
  220.     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
  221.       return exp;
  222.  
  223.     case 'd':  /* A decl node */
  224.       t1 = break_out_calls (DECL_INITIAL (exp));
  225.       if (t1 != DECL_INITIAL (exp))
  226.     {
  227.       exp = copy_node (exp);
  228.       DECL_INITIAL (exp) = t1;
  229.     }
  230.       return exp;
  231.  
  232.     case 'b':  /* A block node */
  233.       {
  234.     /* Don't know how to handle these correctly yet.   Must do a
  235.        break_out_calls on all DECL_INITIAL values for local variables,
  236.        and also break_out_calls on all sub-blocks and sub-statements.  */
  237.     abort ();
  238.       }
  239.       return exp;
  240.  
  241.     case 'e':  /* an expression */
  242.     case 'r':  /* a reference */
  243.     case 's':  /* an expression with side effects */
  244.       for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
  245.     {
  246.       t1 = break_out_calls (TREE_OPERAND (exp, i));
  247.       if (t1 != TREE_OPERAND (exp, i))
  248.         {
  249.           exp = copy_node (exp);
  250.           TREE_OPERAND (exp, i) = t1;
  251.         }
  252.     }
  253.       return exp;
  254.  
  255.     case '<':  /* a comparison expression */
  256.     case '2':  /* a binary arithmetic expression */
  257.       t2 = break_out_calls (TREE_OPERAND (exp, 1));
  258.       if (t2 != TREE_OPERAND (exp, 1))
  259.     changed = 1;
  260.     case '1':  /* a unary arithmetic expression */
  261.       t1 = break_out_calls (TREE_OPERAND (exp, 0));
  262.       if (t1 != TREE_OPERAND (exp, 0))
  263.     changed = 1;
  264.       if (changed)
  265.     {
  266.       if (tree_code_length[(int) code] == 1)
  267.         return build1 (code, TREE_TYPE (exp), t1);
  268.       else
  269.         return build (code, TREE_TYPE (exp), t1, t2);
  270.     }
  271.       return exp;
  272.     }
  273.  
  274. }
  275.  
  276. extern struct obstack *current_obstack;
  277. extern struct obstack permanent_obstack, class_obstack;
  278. extern struct obstack *saveable_obstack;
  279.  
  280. /* Here is how primitive or already-canonicalized types' hash
  281.    codes are made.  MUST BE CONSISTENT WITH tree.c !!! */
  282. #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
  283.  
  284. /* Construct, lay out and return the type of methods belonging to class
  285.    BASETYPE and whose arguments are described by ARGTYPES and whose values
  286.    are described by RETTYPE.  If each type exists already, reuse it.  */
  287. tree
  288. build_cplus_method_type (basetype, rettype, argtypes)
  289.      tree basetype, rettype, argtypes;
  290. {
  291.   register tree t;
  292.   tree ptype;
  293.   int hashcode;
  294.  
  295.   /* Make a node of the sort we want.  */
  296.   t = make_node (METHOD_TYPE);
  297.  
  298.   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
  299.   TREE_TYPE (t) = rettype;
  300.   if (IS_SIGNATURE (basetype))
  301.     ptype = build_signature_pointer_type (TYPE_MAIN_VARIANT (basetype),
  302.                       TYPE_READONLY (basetype),
  303.                       TYPE_VOLATILE (basetype));
  304.   else
  305.     {
  306.       ptype = build_pointer_type (basetype);
  307.       ptype = build_type_variant (ptype, 1, 0);
  308.     }
  309.   /* The actual arglist for this function includes a "hidden" argument
  310.      which is "this".  Put it into the list of argument types.  */
  311.  
  312.   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
  313.   TYPE_ARG_TYPES (t) = argtypes;
  314.   TREE_SIDE_EFFECTS (argtypes) = 1;  /* Mark first argtype as "artificial".  */
  315.  
  316.   /* If we already have such a type, use the old one and free this one.
  317.      Note that it also frees up the above cons cell if found.  */
  318.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
  319.   t = type_hash_canon (hashcode, t);
  320.  
  321.   if (TYPE_SIZE (t) == 0)
  322.     layout_type (t);
  323.  
  324.   return t;
  325. }
  326.  
  327. tree
  328. build_cplus_staticfn_type (basetype, rettype, argtypes)
  329.      tree basetype, rettype, argtypes;
  330. {
  331.   register tree t;
  332.   int hashcode;
  333.  
  334.   /* Make a node of the sort we want.  */
  335.   t = make_node (FUNCTION_TYPE);
  336.  
  337.   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
  338.   TREE_TYPE (t) = rettype;
  339.  
  340.   TYPE_ARG_TYPES (t) = argtypes;
  341.  
  342.   /* If we already have such a type, use the old one and free this one.
  343.      Note that it also frees up the above cons cell if found.  */
  344.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) + type_hash_list (argtypes);
  345.   t = type_hash_canon (hashcode, t);
  346.  
  347.   if (TYPE_SIZE (t) == 0)
  348.     layout_type (t);
  349.  
  350.   return t;
  351. }
  352.  
  353. tree
  354. build_cplus_array_type (elt_type, index_type)
  355.      tree elt_type;
  356.      tree index_type;
  357. {
  358.   register struct obstack *ambient_obstack = current_obstack;
  359.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  360.   tree t;
  361.  
  362.   /* We need a new one.  If both ELT_TYPE and INDEX_TYPE are permanent,
  363.      make this permanent too.  */
  364.   if (TREE_PERMANENT (elt_type)
  365.       && (index_type == 0 || TREE_PERMANENT (index_type)))
  366.     {
  367.       current_obstack = &permanent_obstack;
  368.       saveable_obstack = &permanent_obstack;
  369.     }
  370.  
  371.   t = build_array_type (elt_type, index_type);
  372.  
  373.   /* Push these needs up so that initialization takes place
  374.      more easily.  */
  375.   TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
  376.   TYPE_NEEDS_DESTRUCTOR (t) = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
  377.   current_obstack = ambient_obstack;
  378.   saveable_obstack = ambient_saveable_obstack;
  379.   return t;
  380. }
  381.  
  382. /* Add OFFSET to all base types of T.
  383.  
  384.    OFFSET, which is a type offset, is number of bytes.
  385.  
  386.    Note that we don't have to worry about having two paths to the
  387.    same base type, since this type owns its association list.  */
  388. void
  389. propagate_binfo_offsets (binfo, offset)
  390.      tree binfo;
  391.      tree offset;
  392. {
  393.   tree binfos = BINFO_BASETYPES (binfo);
  394.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  395.  
  396.   for (i = 0; i < n_baselinks; /* note increment is done in the loop.  */)
  397.     {
  398.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  399.  
  400.       if (TREE_VIA_VIRTUAL (base_binfo))
  401.     i += 1;
  402.       else
  403.     {
  404.       int j;
  405.       tree base_binfos = BINFO_BASETYPES (base_binfo);
  406.       tree delta;
  407.  
  408.       for (j = i+1; j < n_baselinks; j++)
  409.         if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
  410.           {
  411.         /* The next basetype offset must take into account the space
  412.            between the classes, not just the size of each class.  */
  413.         delta = size_binop (MINUS_EXPR,
  414.                     BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
  415.                     BINFO_OFFSET (base_binfo));
  416.         break;
  417.           }
  418.  
  419. #if 0
  420.       if (BINFO_OFFSET_ZEROP (base_binfo))
  421.         BINFO_OFFSET (base_binfo) = offset;
  422.       else
  423.         BINFO_OFFSET (base_binfo)
  424.           = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
  425. #else
  426.       BINFO_OFFSET (base_binfo) = offset;
  427. #endif
  428.       if (base_binfos)
  429.         {
  430.           int k;
  431.           tree chain = NULL_TREE;
  432.  
  433.           /* Now unshare the structure beneath BASE_BINFO.  */
  434.           for (k = TREE_VEC_LENGTH (base_binfos)-1;
  435.            k >= 0; k--)
  436.         {
  437.           tree base_base_binfo = TREE_VEC_ELT (base_binfos, k);
  438.           if (! TREE_VIA_VIRTUAL (base_base_binfo))
  439.             TREE_VEC_ELT (base_binfos, k)
  440.               = make_binfo (BINFO_OFFSET (base_base_binfo),
  441.                     base_base_binfo,
  442.                     BINFO_VTABLE (base_base_binfo),
  443.                     BINFO_VIRTUALS (base_base_binfo),
  444.                     chain);
  445.           chain = TREE_VEC_ELT (base_binfos, k);
  446.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
  447.           TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
  448.         }
  449.           /* Now propagate the offset to the base types.  */
  450.           propagate_binfo_offsets (base_binfo, offset);
  451.         }
  452.  
  453.       /* Go to our next class that counts for offset propagation.  */
  454.       i = j;
  455.       if (i < n_baselinks)
  456.         offset = size_binop (PLUS_EXPR, offset, delta);
  457.     }
  458.     }
  459. }
  460.  
  461. /* Compute the actual offsets that our virtual base classes
  462.    will have *for this type*.  This must be performed after
  463.    the fields are laid out, since virtual baseclasses must
  464.    lay down at the end of the record.
  465.  
  466.    Returns the maximum number of virtual functions any of the virtual
  467.    baseclasses provide.  */
  468. int
  469. layout_vbasetypes (rec, max)
  470.      tree rec;
  471.      int max;
  472. {
  473.   /* Get all the virtual base types that this type uses.
  474.      The TREE_VALUE slot holds the virtual baseclass type.  */
  475.   tree vbase_types = get_vbase_types (rec);
  476.  
  477. #ifdef STRUCTURE_SIZE_BOUNDARY
  478.   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  479. #else
  480.   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  481. #endif
  482.   int desired_align;
  483.  
  484.   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
  485.      where CONST_SIZE is an integer
  486.      and VAR_SIZE is a tree expression.
  487.      If VAR_SIZE is null, the size is just CONST_SIZE.
  488.      Naturally we try to avoid using VAR_SIZE.  */
  489.   register unsigned const_size = 0;
  490.   register tree var_size = 0;
  491.   int nonvirtual_const_size;
  492.   tree nonvirtual_var_size;
  493.  
  494.   CLASSTYPE_VBASECLASSES (rec) = vbase_types;
  495.  
  496.   if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  497.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  498.   else
  499.     var_size = TYPE_SIZE (rec);
  500.  
  501.   nonvirtual_const_size = const_size;
  502.   nonvirtual_var_size = var_size;
  503.  
  504.   while (vbase_types)
  505.     {
  506.       tree basetype = BINFO_TYPE (vbase_types);
  507.       tree offset;
  508.  
  509.       desired_align = TYPE_ALIGN (basetype);
  510.       record_align = MAX (record_align, desired_align);
  511.  
  512.       if (const_size == 0)
  513.     offset = integer_zero_node;
  514.       else
  515.     {
  516.       /* Give each virtual base type the alignment it wants.  */
  517.       const_size = CEIL (const_size, TYPE_ALIGN (basetype))
  518.         * TYPE_ALIGN (basetype);
  519.       offset = size_int (CEIL (const_size, BITS_PER_UNIT));
  520.     }
  521.  
  522.       if (CLASSTYPE_VSIZE (basetype) > max)
  523.     max = CLASSTYPE_VSIZE (basetype);
  524.       BINFO_OFFSET (vbase_types) = offset;
  525.  
  526.       if (TREE_CODE (TYPE_SIZE (basetype)) == INTEGER_CST)
  527.     const_size += MAX (BITS_PER_UNIT,
  528.                TREE_INT_CST_LOW (TYPE_SIZE (basetype))
  529.                - TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype)));
  530.       else if (var_size == 0)
  531.     var_size = TYPE_SIZE (basetype);
  532.       else
  533.     var_size = size_binop (PLUS_EXPR, var_size, TYPE_SIZE (basetype));
  534.  
  535.       vbase_types = TREE_CHAIN (vbase_types);
  536.     }
  537.  
  538.   /* Set the alignment in the complete type.  We don't set CLASSTYPE_ALIGN
  539.    here, as that is for this class, without any virtual base classes.  */
  540.   TYPE_ALIGN (rec) = record_align;
  541.   if (const_size != nonvirtual_const_size)
  542.     {
  543.       CLASSTYPE_VBASE_SIZE (rec)
  544.     = size_int (const_size - nonvirtual_const_size);
  545.       TYPE_SIZE (rec) = size_int (const_size);
  546.     }
  547.  
  548.   /* Now propagate offset information throughout the lattice
  549.      under the vbase type.  */
  550.   for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
  551.        vbase_types = TREE_CHAIN (vbase_types))
  552.     {
  553.       tree base_binfos = BINFO_BASETYPES (vbase_types);
  554.  
  555.       if (base_binfos)
  556.     {
  557.       tree chain = NULL_TREE;
  558.       int j;
  559.       /* Now unshare the structure beneath BASE_BINFO.  */
  560.  
  561.       for (j = TREE_VEC_LENGTH (base_binfos)-1;
  562.            j >= 0; j--)
  563.         {
  564.           tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
  565.           if (! TREE_VIA_VIRTUAL (base_base_binfo))
  566.         TREE_VEC_ELT (base_binfos, j)
  567.           = make_binfo (BINFO_OFFSET (base_base_binfo),
  568.                 base_base_binfo,
  569.                 BINFO_VTABLE (base_base_binfo),
  570.                 BINFO_VIRTUALS (base_base_binfo),
  571.                 chain);
  572.           chain = TREE_VEC_ELT (base_binfos, j);
  573.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
  574.           TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
  575.         }
  576.  
  577.       propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
  578.     }
  579.     }
  580.  
  581.   return max;
  582. }
  583.  
  584. /* Lay out the base types of a record type, REC.
  585.    Tentatively set the size and alignment of REC
  586.    according to the base types alone.
  587.  
  588.    Offsets for immediate nonvirtual baseclasses are also computed here.
  589.  
  590.    TYPE_BINFO (REC) should be NULL_TREE on entry, and this routine
  591.    creates a list of base_binfos in TYPE_BINFO (REC) from BINFOS.
  592.  
  593.    Returns list of virtual base classes in a FIELD_DECL chain.  */
  594. tree
  595. layout_basetypes (rec, binfos)
  596.      tree rec, binfos;
  597. {
  598.   /* Chain to hold all the new FIELD_DECLs which point at virtual
  599.      base classes.  */
  600.   tree vbase_decls = NULL_TREE;
  601.  
  602. #ifdef STRUCTURE_SIZE_BOUNDARY
  603.   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  604. #else
  605.   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  606. #endif
  607.  
  608.   /* Record size so far is CONST_SIZE + VAR_SIZE bits, where CONST_SIZE is
  609.      an integer and VAR_SIZE is a tree expression.  If VAR_SIZE is null,
  610.      the size is just CONST_SIZE.  Naturally we try to avoid using
  611.      VAR_SIZE.  And so far, we've been sucessful. */
  612. #if 0
  613.   register tree var_size = 0;
  614. #endif
  615.  
  616.   register unsigned const_size = 0;
  617.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  618.  
  619.   /* Handle basetypes almost like fields, but record their
  620.      offsets differently.  */
  621.  
  622.   for (i = 0; i < n_baseclasses; i++)
  623.     {
  624.       int inc, desired_align, int_vbase_size;
  625.       register tree base_binfo = TREE_VEC_ELT (binfos, i);
  626.       register tree basetype = BINFO_TYPE (base_binfo);
  627.       tree decl, offset;
  628.  
  629.       if (TYPE_SIZE (basetype) == 0)
  630.     {
  631. #if 0
  632.       /* This error is now reported in xref_tag, thus giving better
  633.          location information.  */
  634.       error_with_aggr_type (base_binfo,
  635.                 "base class `%s' has incomplete type");
  636.  
  637.       TREE_VIA_PUBLIC (base_binfo) = 1;
  638.       TREE_VIA_PROTECTED (base_binfo) = 0;
  639.       TREE_VIA_VIRTUAL (base_binfo) = 0;
  640.  
  641.       /* Should handle this better so that
  642.  
  643.          class A;
  644.          class B: private A { virtual void F(); };
  645.  
  646.          does not dump core when compiled. */
  647.       my_friendly_abort (121);
  648. #endif
  649.       continue;
  650.     }
  651.  
  652.       /* All basetypes are recorded in the association list of the
  653.      derived type.  */
  654.  
  655.       if (TREE_VIA_VIRTUAL (base_binfo))
  656.     {
  657.       int j;
  658.       char *name = (char *)alloca (TYPE_NAME_LENGTH (basetype)
  659.                        + sizeof (VBASE_NAME) + 1);
  660.  
  661.       /* The offset for a virtual base class is only used in computing
  662.          virtual function tables and for initializing virtual base
  663.          pointers.  It is built once `get_vbase_types' is called.  */
  664.  
  665.       /* If this basetype can come from another vbase pointer
  666.          without an additional indirection, we will share
  667.          that pointer.  If an indirection is involved, we
  668.          make our own pointer.  */
  669.       for (j = 0; j < n_baseclasses; j++)
  670.         {
  671.           tree other_base_binfo = TREE_VEC_ELT (binfos, j);
  672.           if (! TREE_VIA_VIRTUAL (other_base_binfo)
  673.           && binfo_member (basetype,
  674.                    CLASSTYPE_VBASECLASSES (BINFO_TYPE (other_base_binfo))))
  675.         goto got_it;
  676.         }
  677.       sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (basetype));
  678.       decl = build_lang_decl (FIELD_DECL, get_identifier (name),
  679.                   build_pointer_type (basetype));
  680.       /* If you change any of the below, take a look at all the
  681.          other VFIELD_BASEs and VTABLE_BASEs in the code, and change
  682.          them too. */
  683.       DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
  684.       DECL_VIRTUAL_P (decl) = 1;
  685.       DECL_FIELD_CONTEXT (decl) = rec;
  686.       DECL_CLASS_CONTEXT (decl) = rec;
  687.       DECL_FCONTEXT (decl) = basetype;
  688.       DECL_FIELD_SIZE (decl) = 0;
  689.       DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
  690.       TREE_CHAIN (decl) = vbase_decls;
  691.       BINFO_VPTR_FIELD (base_binfo) = decl;
  692.       vbase_decls = decl;
  693.  
  694.       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
  695.           && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
  696.         {
  697.           warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
  698.                  "destructor `%s' non-virtual");
  699.           warning ("in inheritance relationship `%s: virtual %s'",
  700.                TYPE_NAME_STRING (rec),
  701.                TYPE_NAME_STRING (basetype));
  702.         }
  703.     got_it:
  704.       /* The space this decl occupies has already been accounted for.  */
  705.       continue;
  706.     }
  707.  
  708.       if (const_size == 0)
  709.     offset = integer_zero_node;
  710.       else
  711.     {
  712.       /* Give each base type the alignment it wants.  */
  713.       const_size = CEIL (const_size, TYPE_ALIGN (basetype))
  714.         * TYPE_ALIGN (basetype);
  715.       offset = size_int ((const_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
  716.  
  717. #if 0
  718.       /* bpk: Disabled this check until someone is willing to
  719.          claim it as theirs and explain exactly what circumstances
  720.          warrant the warning.  */ 
  721.       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (basetype)
  722.           && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0)) == NULL_TREE)
  723.         {
  724.           warning_with_decl (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0),
  725.                  "destructor `%s' non-virtual");
  726.           warning ("in inheritance relationship `%s:%s %s'",
  727.                TYPE_NAME_STRING (rec),
  728.                TREE_VIA_VIRTUAL (base_binfo) ? " virtual" : "",
  729.                TYPE_NAME_STRING (basetype));
  730.         }
  731. #endif
  732.     }
  733.       BINFO_OFFSET (base_binfo) = offset;
  734.       if (CLASSTYPE_VSIZE (basetype))
  735.     {
  736.       BINFO_VTABLE (base_binfo) = TYPE_BINFO_VTABLE (basetype);
  737.       BINFO_VIRTUALS (base_binfo) = TYPE_BINFO_VIRTUALS (basetype);
  738.     }
  739.       TREE_CHAIN (base_binfo) = TYPE_BINFO (rec);
  740.       TYPE_BINFO (rec) = base_binfo;
  741.  
  742.       /* Add only the amount of storage not present in
  743.      the virtual baseclasses.  */
  744.  
  745.       int_vbase_size = TREE_INT_CST_LOW (CLASSTYPE_VBASE_SIZE (basetype));
  746.       if (TREE_INT_CST_LOW (TYPE_SIZE (basetype)) > int_vbase_size)
  747.     {
  748.       inc = MAX (record_align,
  749.              (TREE_INT_CST_LOW (TYPE_SIZE (basetype))
  750.               - int_vbase_size));
  751.  
  752.       /* Record must have at least as much alignment as any field.  */
  753.       desired_align = TYPE_ALIGN (basetype);
  754.       record_align = MAX (record_align, desired_align);
  755.  
  756.       const_size += inc;
  757.     }
  758.     }
  759.  
  760.   if (const_size)
  761.     CLASSTYPE_SIZE (rec) = size_int (const_size);
  762.   else
  763.     CLASSTYPE_SIZE (rec) = integer_zero_node;
  764.   CLASSTYPE_ALIGN (rec) = record_align;
  765.  
  766.   return vbase_decls;
  767. }
  768.  
  769. /* Hashing of lists so that we don't make duplicates.
  770.    The entry point is `list_hash_canon'.  */
  771.  
  772. /* Each hash table slot is a bucket containing a chain
  773.    of these structures.  */
  774.  
  775. struct list_hash
  776. {
  777.   struct list_hash *next;    /* Next structure in the bucket.  */
  778.   int hashcode;            /* Hash code of this list.  */
  779.   tree list;            /* The list recorded here.  */
  780. };
  781.  
  782. /* Now here is the hash table.  When recording a list, it is added
  783.    to the slot whose index is the hash code mod the table size.
  784.    Note that the hash table is used for several kinds of lists.
  785.    While all these live in the same table, they are completely independent,
  786.    and the hash code is computed differently for each of these.  */
  787.  
  788. #define TYPE_HASH_SIZE 59
  789. struct list_hash *list_hash_table[TYPE_HASH_SIZE];
  790.  
  791. /* Compute a hash code for a list (chain of TREE_LIST nodes
  792.    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
  793.    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
  794.  
  795. int
  796. list_hash (list)
  797.      tree list;
  798. {
  799.   register int hashcode = 0;
  800.  
  801.   if (TREE_CHAIN (list))
  802.     hashcode += TYPE_HASH (TREE_CHAIN (list));
  803.  
  804.   if (TREE_VALUE (list))
  805.     hashcode += TYPE_HASH (TREE_VALUE (list));
  806.   else
  807.     hashcode += 1007;
  808.   if (TREE_PURPOSE (list))
  809.     hashcode += TYPE_HASH (TREE_PURPOSE (list));
  810.   else
  811.     hashcode += 1009;
  812.   return hashcode;
  813. }
  814.  
  815. /* Look in the type hash table for a type isomorphic to TYPE.
  816.    If one is found, return it.  Otherwise return 0.  */
  817.  
  818. tree
  819. list_hash_lookup (hashcode, list)
  820.      int hashcode;
  821.      tree list;
  822. {
  823.   register struct list_hash *h;
  824.   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  825.     if (h->hashcode == hashcode
  826.     && TREE_VIA_VIRTUAL (h->list) == TREE_VIA_VIRTUAL (list)
  827.     && TREE_VIA_PUBLIC (h->list) == TREE_VIA_PUBLIC (list)
  828.     && TREE_VIA_PROTECTED (h->list) == TREE_VIA_PROTECTED (list)
  829.     && TREE_PURPOSE (h->list) == TREE_PURPOSE (list)
  830.     && TREE_VALUE (h->list) == TREE_VALUE (list)
  831.     && TREE_CHAIN (h->list) == TREE_CHAIN (list))
  832.       {
  833.     my_friendly_assert (TREE_TYPE (h->list) == TREE_TYPE (list), 299);
  834.     return h->list;
  835.       }
  836.   return 0;
  837. }
  838.  
  839. /* Add an entry to the list-hash-table
  840.    for a list TYPE whose hash code is HASHCODE.  */
  841.  
  842. void
  843. list_hash_add (hashcode, list)
  844.      int hashcode;
  845.      tree list;
  846. {
  847.   register struct list_hash *h;
  848.  
  849.   h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
  850.   h->hashcode = hashcode;
  851.   h->list = list;
  852.   h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
  853.   list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
  854. }
  855.  
  856. /* Given TYPE, and HASHCODE its hash code, return the canonical
  857.    object for an identical list if one already exists.
  858.    Otherwise, return TYPE, and record it as the canonical object
  859.    if it is a permanent object.
  860.  
  861.    To use this function, first create a list of the sort you want.
  862.    Then compute its hash code from the fields of the list that
  863.    make it different from other similar lists.
  864.    Then call this function and use the value.
  865.    This function frees the list you pass in if it is a duplicate.  */
  866.  
  867. /* Set to 1 to debug without canonicalization.  Never set by program.  */
  868. static int debug_no_list_hash = 0;
  869.  
  870. tree
  871. list_hash_canon (hashcode, list)
  872.      int hashcode;
  873.      tree list;
  874. {
  875.   tree t1;
  876.  
  877.   if (debug_no_list_hash)
  878.     return list;
  879.  
  880.   t1 = list_hash_lookup (hashcode, list);
  881.   if (t1 != 0)
  882.     {
  883.       obstack_free (&class_obstack, list);
  884.       return t1;
  885.     }
  886.  
  887.   /* If this is a new list, record it for later reuse.  */
  888.   list_hash_add (hashcode, list);
  889.  
  890.   return list;
  891. }
  892.  
  893. tree
  894. hash_tree_cons (via_public, via_virtual, via_protected, purpose, value, chain)
  895.      int via_public, via_virtual, via_protected;
  896.      tree purpose, value, chain;
  897. {
  898.   struct obstack *ambient_obstack = current_obstack;
  899.   tree t;
  900.   int hashcode;
  901.  
  902.   current_obstack = &class_obstack;
  903.   t = tree_cons (purpose, value, chain);
  904.   TREE_VIA_PUBLIC (t) = via_public;
  905.   TREE_VIA_PROTECTED (t) = via_protected;
  906.   TREE_VIA_VIRTUAL (t) = via_virtual;
  907.   hashcode = list_hash (t);
  908.   t = list_hash_canon (hashcode, t);
  909.   current_obstack = ambient_obstack;
  910.   return t;
  911. }
  912.  
  913. /* Constructor for hashed lists.  */
  914. tree
  915. hash_tree_chain (value, chain)
  916.      tree value, chain;
  917. {
  918.   struct obstack *ambient_obstack = current_obstack;
  919.   tree t;
  920.   int hashcode;
  921.  
  922.   current_obstack = &class_obstack;
  923.   t = tree_cons (NULL_TREE, value, chain);
  924.   hashcode = list_hash (t);
  925.   t = list_hash_canon (hashcode, t);
  926.   current_obstack = ambient_obstack;
  927.   return t;
  928. }
  929.  
  930. /* Similar, but used for concatenating two lists.  */
  931. tree
  932. hash_chainon (list1, list2)
  933.      tree list1, list2;
  934. {
  935.   if (list2 == 0)
  936.     return list1;
  937.   if (list1 == 0)
  938.     return list2;
  939.   if (TREE_CHAIN (list1) == NULL_TREE)
  940.     return hash_tree_chain (TREE_VALUE (list1), list2);
  941.   return hash_tree_chain (TREE_VALUE (list1),
  942.               hash_chainon (TREE_CHAIN (list1), list2));
  943. }
  944.  
  945. static tree
  946. get_identifier_list (value)
  947.      tree value;
  948. {
  949.   tree list = IDENTIFIER_AS_LIST (value);
  950.   if (list != NULL_TREE
  951.       && (TREE_CODE (list) != TREE_LIST
  952.       || TREE_VALUE (list) != value))
  953.     list = NULL_TREE;
  954.   else if (IDENTIFIER_HAS_TYPE_VALUE (value)
  955.        && TREE_CODE (IDENTIFIER_TYPE_VALUE (value)) == RECORD_TYPE
  956.        && IDENTIFIER_TYPE_VALUE (value)
  957.           == TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (value)))
  958.     {
  959.       tree type = IDENTIFIER_TYPE_VALUE (value);
  960.  
  961.       if (TYPE_PTRMEMFUNC_P (type))
  962.     list = NULL_TREE;
  963.       else if (type == current_class_type)
  964.     /* Don't mess up the constructor name.  */
  965.     list = tree_cons (NULL_TREE, value, NULL_TREE);
  966.       else
  967.     {
  968.       register tree id;
  969.       /* This will return the correct thing for regular types,
  970.          nested types, and templates.  Yay! */
  971.       if (TYPE_NESTED_NAME (type))
  972.         id = TYPE_NESTED_NAME (type);
  973.       else
  974.         id = TYPE_IDENTIFIER (type);
  975.  
  976.       if (CLASSTYPE_ID_AS_LIST (type) == NULL_TREE)
  977.         CLASSTYPE_ID_AS_LIST (type)
  978.           = perm_tree_cons (NULL_TREE, id, NULL_TREE);
  979.       list = CLASSTYPE_ID_AS_LIST (type);
  980.     }
  981.     }
  982.   return list;
  983. }
  984.  
  985. tree
  986. get_decl_list (value)
  987.      tree value;
  988. {
  989.   tree list = NULL_TREE;
  990.  
  991.   if (TREE_CODE (value) == IDENTIFIER_NODE)
  992.     list = get_identifier_list (value);
  993.   else if (TREE_CODE (value) == RECORD_TYPE
  994.        && TYPE_LANG_SPECIFIC (value))
  995.     list = CLASSTYPE_AS_LIST (value);
  996.  
  997.   if (list != NULL_TREE)
  998.     {
  999.       my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 301);
  1000.       return list;
  1001.     }
  1002.  
  1003.   return build_decl_list (NULL_TREE, value);
  1004. }
  1005.  
  1006. /* Look in the type hash table for a type isomorphic to
  1007.    `build_tree_list (NULL_TREE, VALUE)'.
  1008.    If one is found, return it.  Otherwise return 0.  */
  1009.  
  1010. tree
  1011. list_hash_lookup_or_cons (value)
  1012.      tree value;
  1013. {
  1014.   register int hashcode = TYPE_HASH (value);
  1015.   register struct list_hash *h;
  1016.   struct obstack *ambient_obstack;
  1017.   tree list = NULL_TREE;
  1018.  
  1019.   if (TREE_CODE (value) == IDENTIFIER_NODE)
  1020.     list = get_identifier_list (value);
  1021.   else if (TREE_CODE (value) == TYPE_DECL
  1022.        && TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
  1023.        && TYPE_LANG_SPECIFIC (TREE_TYPE (value)))
  1024.     list = CLASSTYPE_ID_AS_LIST (TREE_TYPE (value));
  1025.   else if (TREE_CODE (value) == RECORD_TYPE
  1026.        && TYPE_LANG_SPECIFIC (value))
  1027.     list = CLASSTYPE_AS_LIST (value);
  1028.  
  1029.   if (list != NULL_TREE)
  1030.     {
  1031.       my_friendly_assert (TREE_CHAIN (list) == NULL_TREE, 302);
  1032.       return list;
  1033.     }
  1034.  
  1035.   if (debug_no_list_hash)
  1036.     return hash_tree_chain (value, NULL_TREE);
  1037.  
  1038.   for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  1039.     if (h->hashcode == hashcode
  1040.     && TREE_VIA_VIRTUAL (h->list) == 0
  1041.     && TREE_VIA_PUBLIC (h->list) == 0
  1042.     && TREE_VIA_PROTECTED (h->list) == 0
  1043.     && TREE_PURPOSE (h->list) == 0
  1044.     && TREE_VALUE (h->list) == value)
  1045.       {
  1046.     my_friendly_assert (TREE_TYPE (h->list) == 0, 303);
  1047.     my_friendly_assert (TREE_CHAIN (h->list) == 0, 304);
  1048.     return h->list;
  1049.       }
  1050.  
  1051.   ambient_obstack = current_obstack;
  1052.   current_obstack = &class_obstack;
  1053.   list = build_tree_list (NULL_TREE, value);
  1054.   list_hash_add (hashcode, list);
  1055.   current_obstack = ambient_obstack;
  1056.   return list;
  1057. }
  1058.  
  1059. /* Build an association between TYPE and some parameters:
  1060.  
  1061.    OFFSET is the offset added to `this' to convert it to a pointer
  1062.    of type `TYPE *'
  1063.  
  1064.    BINFO is the base binfo to use, if we are deriving from one.  This
  1065.    is necessary, as we want specialized parent binfos from base
  1066.    classes, so that the VTABLE_NAMEs of bases are for the most derived
  1067.    type, instead of of the simple type.
  1068.  
  1069.    VTABLE is the virtual function table with which to initialize
  1070.    sub-objects of type TYPE.
  1071.  
  1072.    VIRTUALS are the virtual functions sitting in VTABLE.
  1073.  
  1074.    CHAIN are more associations we must retain.  */
  1075.  
  1076. tree
  1077. make_binfo (offset, binfo, vtable, virtuals, chain)
  1078.      tree offset, binfo;
  1079.      tree vtable, virtuals;
  1080.      tree chain;
  1081. {
  1082.   tree new_binfo = make_tree_vec (6);
  1083.   tree type;
  1084.  
  1085.   if (TREE_CODE (binfo) == TREE_VEC)
  1086.     type = BINFO_TYPE (binfo);
  1087.   else
  1088.     {
  1089.       type = binfo;
  1090.       binfo = TYPE_BINFO (binfo);
  1091.     }
  1092.  
  1093.   TREE_CHAIN (new_binfo) = chain;
  1094.   if (chain)
  1095.     TREE_USED (new_binfo) = TREE_USED (chain);
  1096.  
  1097.   TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
  1098.   BINFO_OFFSET (new_binfo) = offset;
  1099.   BINFO_VTABLE (new_binfo) = vtable;
  1100.   BINFO_VIRTUALS (new_binfo) = virtuals;
  1101.   BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
  1102.  
  1103.   if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
  1104.     BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));      
  1105.   return new_binfo;
  1106. }
  1107.  
  1108. tree
  1109. copy_binfo (list)
  1110.      tree list;
  1111. {
  1112.   tree binfo = copy_list (list);
  1113.   tree rval = binfo;
  1114.   while (binfo)
  1115.     {
  1116.       TREE_USED (binfo) = 0;
  1117.       if (BINFO_BASETYPES (binfo))
  1118.     BINFO_BASETYPES (binfo) = copy_node (BINFO_BASETYPES (binfo));
  1119.       binfo = TREE_CHAIN (binfo);
  1120.     }
  1121.   return rval;
  1122. }
  1123.  
  1124. /* Return the binfo value for ELEM in TYPE.  */
  1125.  
  1126. tree
  1127. binfo_value (elem, type)
  1128.      tree elem;
  1129.      tree type;
  1130. {
  1131.   if (get_base_distance (elem, type, 0, (tree *)0) == -2)
  1132.     compiler_error ("base class `%s' ambiguous in binfo_value",
  1133.             TYPE_NAME_STRING (elem));
  1134.   if (elem == type)
  1135.     return TYPE_BINFO (type);
  1136.   if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
  1137.     return type;
  1138.   return get_binfo (elem, type, 0);
  1139. }
  1140.  
  1141. tree
  1142. reverse_path (path)
  1143.      tree path;
  1144. {
  1145.   register tree prev = 0, tmp, next;
  1146.   for (tmp = path; tmp; tmp = next)
  1147.     {
  1148.       next = BINFO_INHERITANCE_CHAIN (tmp);
  1149.       BINFO_INHERITANCE_CHAIN (tmp) = prev;
  1150.       prev = tmp;
  1151.     }
  1152.   return prev;
  1153. }
  1154.  
  1155. tree
  1156. virtual_member (elem, list)
  1157.      tree elem;
  1158.      tree list;
  1159. {
  1160.   tree t;
  1161.   tree rval, nval;
  1162.  
  1163.   for (t = list; t; t = TREE_CHAIN (t))
  1164.     if (elem == BINFO_TYPE (t))
  1165.       return t;
  1166.   rval = 0;
  1167.   for (t = list; t; t = TREE_CHAIN (t))
  1168.     {
  1169.       tree binfos = BINFO_BASETYPES (t);
  1170.       int i;
  1171.  
  1172.       if (binfos != NULL_TREE)
  1173.     for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1174.       {
  1175.         nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
  1176.         if (nval)
  1177.           {
  1178.         if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
  1179.           my_friendly_abort (104);
  1180.         rval = nval;
  1181.           }
  1182.       }
  1183.     }
  1184.   return rval;
  1185. }
  1186.  
  1187. /* Return the offset (as an INTEGER_CST) for ELEM in LIST.
  1188.    INITIAL_OFFSET is the value to add to the offset that ELEM's
  1189.    binfo entry in LIST provides.
  1190.  
  1191.    Returns NULL if ELEM does not have an binfo value in LIST.  */
  1192.  
  1193. tree
  1194. virtual_offset (elem, list, initial_offset)
  1195.      tree elem;
  1196.      tree list;
  1197.      tree initial_offset;
  1198. {
  1199.   tree vb, offset;
  1200.   tree rval, nval;
  1201.  
  1202.   for (vb = list; vb; vb = TREE_CHAIN (vb))
  1203.     if (elem == BINFO_TYPE (vb))
  1204.       return size_binop (PLUS_EXPR, initial_offset, BINFO_OFFSET (vb));
  1205.   rval = 0;
  1206.   for (vb = list; vb; vb = TREE_CHAIN (vb))
  1207.     {
  1208.       tree binfos = BINFO_BASETYPES (vb);
  1209.       int i;
  1210.  
  1211.       if (binfos == NULL_TREE)
  1212.     continue;
  1213.  
  1214.       for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1215.     {
  1216.       nval = binfo_value (elem, BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
  1217.       if (nval)
  1218.         {
  1219.           if (rval && BINFO_OFFSET (nval) != BINFO_OFFSET (rval))
  1220.         my_friendly_abort (105);
  1221.           offset = BINFO_OFFSET (vb);
  1222.           rval = nval;
  1223.         }
  1224.     }
  1225.     }
  1226.   if (rval == NULL_TREE)
  1227.     return rval;
  1228.   return size_binop (PLUS_EXPR, offset, BINFO_OFFSET (rval));
  1229. }
  1230.  
  1231. void
  1232. debug_binfo (elem)
  1233.      tree elem;
  1234. {
  1235.   int i;
  1236.   tree virtuals;
  1237.  
  1238.   fprintf (stderr, "type \"%s\"; offset = %d\n",
  1239.        TYPE_NAME_STRING (BINFO_TYPE (elem)),
  1240.        TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
  1241.   fprintf (stderr, "vtable type:\n");
  1242.   debug_tree (BINFO_TYPE (elem));
  1243.   if (BINFO_VTABLE (elem))
  1244.     fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
  1245.   else
  1246.     fprintf (stderr, "no vtable decl yet\n");
  1247.   fprintf (stderr, "virtuals:\n");
  1248.   virtuals = BINFO_VIRTUALS (elem);
  1249.   if (virtuals != 0)
  1250.     {
  1251.       virtuals = TREE_CHAIN (virtuals);
  1252.       if (flag_dossier)
  1253.     virtuals = TREE_CHAIN (virtuals);
  1254.     }
  1255.   i = 1;
  1256.   while (virtuals)
  1257.     {
  1258.       tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  1259.       fprintf (stderr, "%s [%d =? %d]\n",
  1260.            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
  1261.            i, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
  1262.       virtuals = TREE_CHAIN (virtuals);
  1263.       i += 1;
  1264.     }
  1265. }
  1266.  
  1267. /* Return the length of a chain of nodes chained through DECL_CHAIN.
  1268.    We expect a null pointer to mark the end of the chain.
  1269.    This is the Lisp primitive `length'.  */
  1270.  
  1271. int
  1272. decl_list_length (t)
  1273.      tree t;
  1274. {
  1275.   register tree tail;
  1276.   register int len = 0;
  1277.  
  1278.   my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
  1279.               || TREE_CODE (t) == TEMPLATE_DECL, 300);
  1280.   for (tail = t; tail; tail = DECL_CHAIN (tail))
  1281.     len++;
  1282.  
  1283.   return len;
  1284. }
  1285.  
  1286. int
  1287. count_functions (t)
  1288.      tree t;
  1289. {
  1290.   if (TREE_CODE (t) == FUNCTION_DECL)
  1291.     return 1;
  1292.   else if (TREE_CODE (t) == TREE_LIST)
  1293.     return decl_list_length (TREE_VALUE (t));
  1294.  
  1295.   my_friendly_abort (359);
  1296.   return 0;
  1297. }
  1298.  
  1299. /* Like value_member, but for DECL_CHAINs.  */
  1300. tree
  1301. decl_value_member (elem, list)
  1302.      tree elem, list;
  1303. {
  1304.   while (list)
  1305.     {
  1306.       if (elem == list)
  1307.     return list;
  1308.       list = DECL_CHAIN (list);
  1309.     }
  1310.   return NULL_TREE;
  1311. }
  1312.  
  1313. int
  1314. is_overloaded_fn (x)
  1315.      tree x;
  1316. {
  1317.   if (TREE_CODE (x) == FUNCTION_DECL)
  1318.     return 1;
  1319.  
  1320.   if (TREE_CODE (x) == TREE_LIST
  1321.       && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
  1322.       || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
  1323.     return 1;
  1324.  
  1325.   return 0;
  1326. }
  1327.  
  1328. int
  1329. really_overloaded_fn (x)
  1330.      tree x;
  1331. {     
  1332.   if (TREE_CODE (x) == TREE_LIST
  1333.       && (TREE_CODE (TREE_VALUE (x)) == FUNCTION_DECL
  1334.       || TREE_CODE (TREE_VALUE (x)) == TEMPLATE_DECL))
  1335.     return 1;
  1336.  
  1337.   return 0;
  1338. }
  1339.  
  1340. tree
  1341. get_first_fn (from)
  1342.      tree from;
  1343. {
  1344.   if (TREE_CODE (from) == FUNCTION_DECL)
  1345.     return from;
  1346.  
  1347.   my_friendly_assert (TREE_CODE (from) == TREE_LIST, 9);
  1348.   
  1349.   return TREE_VALUE (from);
  1350. }
  1351.  
  1352. tree
  1353. fnaddr_from_vtable_entry (entry)
  1354.      tree entry;
  1355. {
  1356.   if (flag_vtable_thunks)
  1357.     {
  1358.       tree func = entry;
  1359.       if (TREE_CODE (func) == ADDR_EXPR)
  1360.     func = TREE_OPERAND (func, 0);
  1361.       if (TREE_CODE (func) == THUNK_DECL)
  1362.     return DECL_INITIAL (func);
  1363.       else
  1364.     return entry;
  1365.     }
  1366.   else
  1367.     return TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry))));
  1368. }
  1369.  
  1370. void
  1371. set_fnaddr_from_vtable_entry (entry, value)
  1372.      tree entry, value;
  1373. {
  1374.   if (flag_vtable_thunks)
  1375.     abort ();
  1376.   else
  1377.   TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (entry)))) = value;
  1378. }
  1379.  
  1380. tree
  1381. function_arg_chain (t)
  1382.      tree t;
  1383. {
  1384.   return TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (t)));
  1385. }
  1386.  
  1387. int
  1388. promotes_to_aggr_type (t, code)
  1389.      tree t;
  1390.      enum tree_code code;
  1391. {
  1392.   if (TREE_CODE (t) == code)
  1393.     t = TREE_TYPE (t);
  1394.   return IS_AGGR_TYPE (t);
  1395. }
  1396.  
  1397. int
  1398. is_aggr_type_2 (t1, t2)
  1399.      tree t1, t2;
  1400. {
  1401.   if (TREE_CODE (t1) != TREE_CODE (t2))
  1402.     return 0;
  1403.   return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
  1404. }
  1405.  
  1406. /* Give message using types TYPE1 and TYPE2 as arguments.
  1407.    PFN is the function which will print the message;
  1408.    S is the format string for PFN to use.  */
  1409. void
  1410. message_2_types (pfn, s, type1, type2)
  1411.      void (*pfn) ();
  1412.      char *s;
  1413.      tree type1, type2;
  1414. {
  1415.   tree name1 = TYPE_NAME (type1);
  1416.   tree name2 = TYPE_NAME (type2);
  1417.   if (TREE_CODE (name1) == TYPE_DECL)
  1418.     name1 = DECL_NAME (name1);
  1419.   if (TREE_CODE (name2) == TYPE_DECL)
  1420.     name2 = DECL_NAME (name2);
  1421.   (*pfn) (s, IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
  1422. }
  1423.  
  1424. #define PRINT_RING_SIZE 4
  1425.  
  1426. char *
  1427. lang_printable_name (decl)
  1428.      tree decl;
  1429. {
  1430.   static tree decl_ring[PRINT_RING_SIZE];
  1431.   static char *print_ring[PRINT_RING_SIZE];
  1432.   static int ring_counter;
  1433.   int i;
  1434.  
  1435.   /* Only cache functions.  */
  1436.   if (TREE_CODE (decl) != FUNCTION_DECL
  1437.       || DECL_LANG_SPECIFIC (decl) == 0)
  1438.     return decl_as_string (decl, 1);
  1439.  
  1440.   /* See if this print name is lying around.  */
  1441.   for (i = 0; i < PRINT_RING_SIZE; i++)
  1442.     if (decl_ring[i] == decl)
  1443.       /* yes, so return it.  */
  1444.       return print_ring[i];
  1445.  
  1446.   if (++ring_counter == PRINT_RING_SIZE)
  1447.     ring_counter = 0;
  1448.  
  1449.   if (current_function_decl != NULL_TREE)
  1450.     {
  1451.       if (decl_ring[ring_counter] == current_function_decl)
  1452.     ring_counter += 1;
  1453.       if (ring_counter == PRINT_RING_SIZE)
  1454.     ring_counter = 0;
  1455.       if (decl_ring[ring_counter] == current_function_decl)
  1456.     my_friendly_abort (106);
  1457.     }
  1458.  
  1459.   if (print_ring[ring_counter])
  1460.     free (print_ring[ring_counter]);
  1461.  
  1462.   {
  1463.     int print_ret_type_p
  1464.       = (!DECL_CONSTRUCTOR_P (decl)
  1465.      && !DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
  1466.  
  1467.     char *name = (char *)decl_as_string (decl, print_ret_type_p);
  1468.     print_ring[ring_counter] = (char *)malloc (strlen (name) + 1);
  1469.     strcpy (print_ring[ring_counter], name);
  1470.     decl_ring[ring_counter] = decl;
  1471.   }
  1472.   return print_ring[ring_counter];
  1473. }
  1474.  
  1475. /* Comparison function for sorting identifiers in RAISES lists.
  1476.    Note that because IDENTIFIER_NODEs are unique, we can sort
  1477.    them by address, saving an indirection.  */
  1478. static int
  1479. id_cmp (p1, p2)
  1480.      tree *p1, *p2;
  1481. {
  1482.   return (HOST_WIDE_INT)TREE_VALUE (*p1) - (HOST_WIDE_INT)TREE_VALUE (*p2);
  1483. }
  1484.  
  1485. /* Build the FUNCTION_TYPE or METHOD_TYPE which may raise exceptions
  1486.    listed in RAISES.  */
  1487. tree
  1488. build_exception_variant (ctype, type, raises)
  1489.      tree ctype, type;
  1490.      tree raises;
  1491. {
  1492.   int i;
  1493.   tree v = TYPE_MAIN_VARIANT (type);
  1494.   tree t, t2, cname;
  1495.   tree *a = (tree *)alloca ((list_length (raises)+1) * sizeof (tree));
  1496.   int constp = TYPE_READONLY (type);
  1497.   int volatilep = TYPE_VOLATILE (type);
  1498.  
  1499.   for (v = TYPE_NEXT_VARIANT (v); v; v = TYPE_NEXT_VARIANT (v))
  1500.     {
  1501.       if (TYPE_READONLY (v) != constp
  1502.       || TYPE_VOLATILE (v) != volatilep)
  1503.     continue;
  1504.  
  1505.       t = raises;
  1506.       t2 = TYPE_RAISES_EXCEPTIONS (v);
  1507.       while (t && t2)
  1508.     {
  1509.       if (TREE_TYPE (t) == TREE_TYPE (t2))
  1510.         {
  1511.           t = TREE_CHAIN (t);
  1512.           t2 = TREE_CHAIN (t2);
  1513.         }
  1514.       else break;
  1515.     }
  1516.       if (t || t2)
  1517.     continue;
  1518.       /* List of exceptions raised matches previously found list.
  1519.  
  1520.          @@ Nice to free up storage used in consing up the
  1521.      @@ list of exceptions raised.  */
  1522.       return v;
  1523.     }
  1524.  
  1525.   /* Need to build a new variant.  */
  1526.   v = copy_node (type);
  1527.   TYPE_NEXT_VARIANT (v) = TYPE_NEXT_VARIANT (type);
  1528.   TYPE_NEXT_VARIANT (type) = v;
  1529.   if (raises && ! TREE_PERMANENT (raises))
  1530.     {
  1531.       push_obstacks_nochange ();
  1532.       end_temporary_allocation ();
  1533.       raises = copy_list (raises);
  1534.       pop_obstacks ();
  1535.     }
  1536.   TYPE_RAISES_EXCEPTIONS (v) = raises;
  1537.   return v;
  1538. }
  1539.  
  1540. /* Subroutine of copy_to_permanent
  1541.  
  1542.    Assuming T is a node build bottom-up, make it all exist on
  1543.    permanent obstack, if it is not permanent already.  */
  1544. static tree
  1545. make_deep_copy (t)
  1546.      tree t;
  1547. {
  1548.   enum tree_code code;
  1549.  
  1550.   if (t == NULL_TREE || TREE_PERMANENT (t))
  1551.     return t;
  1552.  
  1553.   switch (code = TREE_CODE (t))
  1554.     {
  1555.     case ERROR_MARK:
  1556.       return error_mark_node;
  1557.  
  1558.     case VAR_DECL:
  1559.     case FUNCTION_DECL:
  1560.     case CONST_DECL:
  1561.       break;
  1562.  
  1563.     case PARM_DECL:
  1564.       {
  1565.     tree chain = TREE_CHAIN (t);
  1566.     t = copy_node (t);
  1567.     TREE_CHAIN (t) = make_deep_copy (chain);
  1568.     TREE_TYPE (t) = make_deep_copy (TREE_TYPE (t));
  1569.     DECL_INITIAL (t) = make_deep_copy (DECL_INITIAL (t));
  1570.     DECL_SIZE (t) = make_deep_copy (DECL_SIZE (t));
  1571.     return t;
  1572.       }
  1573.  
  1574.     case TREE_LIST:
  1575.       {
  1576.     tree chain = TREE_CHAIN (t);
  1577.     t = copy_node (t);
  1578.     TREE_PURPOSE (t) = make_deep_copy (TREE_PURPOSE (t));
  1579.     TREE_VALUE (t) = make_deep_copy (TREE_VALUE (t));
  1580.     TREE_CHAIN (t) = make_deep_copy (chain);
  1581.     return t;
  1582.       }
  1583.  
  1584.     case TREE_VEC:
  1585.       {
  1586.     int len = TREE_VEC_LENGTH (t);
  1587.  
  1588.     t = copy_node (t);
  1589.     while (len--)
  1590.       TREE_VEC_ELT (t, len) = make_deep_copy (TREE_VEC_ELT (t, len));
  1591.     return t;
  1592.       }
  1593.  
  1594.     case INTEGER_CST:
  1595.     case REAL_CST:
  1596.     case STRING_CST:
  1597.       return copy_node (t);
  1598.  
  1599.     case COND_EXPR:
  1600.     case TARGET_EXPR:
  1601.     case NEW_EXPR:
  1602.       t = copy_node (t);
  1603.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1604.       TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
  1605.       TREE_OPERAND (t, 2) = make_deep_copy (TREE_OPERAND (t, 2));
  1606.       return t;
  1607.  
  1608.     case SAVE_EXPR:
  1609.       t = copy_node (t);
  1610.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1611.       return t;
  1612.  
  1613.     case MODIFY_EXPR:
  1614.     case PLUS_EXPR:
  1615.     case MINUS_EXPR:
  1616.     case MULT_EXPR:
  1617.     case TRUNC_DIV_EXPR:
  1618.     case TRUNC_MOD_EXPR:
  1619.     case MIN_EXPR:
  1620.     case MAX_EXPR:
  1621.     case LSHIFT_EXPR:
  1622.     case RSHIFT_EXPR:
  1623.     case BIT_IOR_EXPR:
  1624.     case BIT_XOR_EXPR:
  1625.     case BIT_AND_EXPR:
  1626.     case BIT_ANDTC_EXPR:
  1627.     case TRUTH_ANDIF_EXPR:
  1628.     case TRUTH_ORIF_EXPR:
  1629.     case LT_EXPR:
  1630.     case LE_EXPR:
  1631.     case GT_EXPR:
  1632.     case GE_EXPR:
  1633.     case EQ_EXPR:
  1634.     case NE_EXPR:
  1635.     case CEIL_DIV_EXPR:
  1636.     case FLOOR_DIV_EXPR:
  1637.     case ROUND_DIV_EXPR:
  1638.     case CEIL_MOD_EXPR:
  1639.     case FLOOR_MOD_EXPR:
  1640.     case ROUND_MOD_EXPR:
  1641.     case COMPOUND_EXPR:
  1642.     case PREDECREMENT_EXPR:
  1643.     case PREINCREMENT_EXPR:
  1644.     case POSTDECREMENT_EXPR:
  1645.     case POSTINCREMENT_EXPR:
  1646.     case CALL_EXPR:
  1647.       t = copy_node (t);
  1648.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1649.       TREE_OPERAND (t, 1) = make_deep_copy (TREE_OPERAND (t, 1));
  1650.       return t;
  1651.  
  1652.     case CONVERT_EXPR:
  1653.     case ADDR_EXPR:
  1654.     case INDIRECT_REF:
  1655.     case NEGATE_EXPR:
  1656.     case BIT_NOT_EXPR:
  1657.     case TRUTH_NOT_EXPR:
  1658.     case NOP_EXPR:
  1659.     case COMPONENT_REF:
  1660.       t = copy_node (t);
  1661.       TREE_OPERAND (t, 0) = make_deep_copy (TREE_OPERAND (t, 0));
  1662.       return t;
  1663.  
  1664.       /*  This list is incomplete, but should suffice for now.
  1665.       It is very important that `sorry' does not call
  1666.       `report_error_function'.  That could cause an infinite loop.  */
  1667.     default:
  1668.       sorry ("initializer contains unrecognized tree code");
  1669.       return error_mark_node;
  1670.  
  1671.     }
  1672.   my_friendly_abort (107);
  1673.   /* NOTREACHED */
  1674.   return NULL_TREE;
  1675. }
  1676.  
  1677. /* Assuming T is a node built bottom-up, make it all exist on
  1678.    permanent obstack, if it is not permanent already.  */
  1679. tree
  1680. copy_to_permanent (t)
  1681.      tree t;
  1682. {
  1683.   register struct obstack *ambient_obstack = current_obstack;
  1684.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1685.  
  1686.   if (t == NULL_TREE || TREE_PERMANENT (t))
  1687.     return t;
  1688.  
  1689.   saveable_obstack = &permanent_obstack;
  1690.   current_obstack = saveable_obstack;
  1691.  
  1692.   t = make_deep_copy (t);
  1693.  
  1694.   current_obstack = ambient_obstack;
  1695.   saveable_obstack = ambient_saveable_obstack;
  1696.  
  1697.   return t;
  1698. }
  1699.  
  1700. void
  1701. print_lang_statistics ()
  1702. {
  1703.   extern struct obstack maybepermanent_obstack;
  1704.   print_obstack_statistics ("class_obstack", &class_obstack);
  1705.   print_obstack_statistics ("permanent_obstack", &permanent_obstack);
  1706.   print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
  1707.   print_search_statistics ();
  1708.   print_class_statistics ();
  1709. }
  1710.  
  1711. /* This is used by the `assert' macro.  It is provided in libgcc.a,
  1712.    which `cc' doesn't know how to link.  Note that the C++ front-end
  1713.    no longer actually uses the `assert' macro (instead, it calls
  1714.    my_friendly_assert).  But all of the back-end files still need this.  */
  1715. void
  1716. __eprintf (string, expression, line, filename)
  1717. #ifdef __STDC__
  1718.      const char *string;
  1719.      const char *expression;
  1720.      unsigned line;
  1721.      const char *filename;
  1722. #else
  1723.      char *string;
  1724.      char *expression;
  1725.      unsigned line;
  1726.      char *filename;
  1727. #endif
  1728. {
  1729.   fprintf (stderr, string, expression, line, filename);
  1730.   fflush (stderr);
  1731.   abort ();
  1732. }
  1733.  
  1734. /* Return, as an INTEGER_CST node, the number of elements for
  1735.    TYPE (which is an ARRAY_TYPE).  This counts only elements of the top array. */
  1736.  
  1737. tree
  1738. array_type_nelts_top (type)
  1739.      tree type;
  1740. {
  1741.   return fold (build (PLUS_EXPR, integer_type_node,
  1742.               array_type_nelts (type),
  1743.               integer_one_node));
  1744. }
  1745.  
  1746. /* Return, as an INTEGER_CST node, the number of elements for
  1747.    TYPE (which is an ARRAY_TYPE).  This one is a recursive count of all
  1748.    ARRAY_TYPEs that are clumped together. */
  1749.  
  1750. tree
  1751. array_type_nelts_total (type)
  1752.      tree type;
  1753. {
  1754.   tree sz = array_type_nelts_top (type);
  1755.   type = TREE_TYPE (type);
  1756.   while (TREE_CODE (type) == ARRAY_TYPE)
  1757.     {
  1758.       tree n = array_type_nelts_top (type);
  1759.       sz = fold (build (MULT_EXPR, integer_type_node, sz, n));
  1760.       type = TREE_TYPE (type);
  1761.     }
  1762.   return sz;
  1763. }
  1764.